#include <iostream>
#include <fstream>
#include <string>
#include "contacts.pb.h"
using namespace std;
//using namespace contacts;

void AddPeopleInfo(contacts::PeopleInfo *people_info_ptr){
    cout << "---------------新增联系人--------------" << endl;

    cout << "请输入联系人名字：" ;
    string name;
    getline(cin, name);
    people_info_ptr->set_name(name);

    cout << "请输入年龄：" ;
    int age;
    cin >> age;
    people_info_ptr->set_age(age);

    cin.ignore(256, '\n');

    cout << "请输入职业：" ;
    string profession;
    getline(cin, profession);
    people_info_ptr->set_profession(profession);

    cout << "请输入薪水：" ;
    string salary;
    getline(cin, salary);
    people_info_ptr->set_salary(salary);
    //cin.ignore(256, '\n');

    for(int i = 0; ; i++){
        cout << "请输入联系人电话" << i + 1 << "（只需输入回车完成新增）：" ;
        string number;
        getline(cin, number);
        if(number.empty())
            break; 
        contacts::PeopleInfo_Phone *phone = people_info_ptr->add_phone();
        phone->set_number(number);

        bool flag = true;
        while(flag){
            cout << "请输入电话类型（1：固定电话0754... ； 2：移动电话）" ;
            int type;
            cin >> type;
            cin.ignore(256,'\n');
            switch (type)
            {
            case 1:
                phone->set_type(contacts::PeopleInfo_Phone_PhoneType::PeopleInfo_Phone_PhoneType_MB);
                flag = false;
                break;
            case 2:
                phone->set_type(contacts::PeopleInfo_Phone_PhoneType::PeopleInfo_Phone_PhoneType_TEL);
                flag = false;
                break;
            default:
                cout << "输入有误，请在1和2里选择电话类型，1代表固定电话，2代表移动电话" << endl;
                continue;;
            }
        }

    }

    contacts::Address address;
    // 添加联系人地址
    string home_address, unit_address; 
    cout << "请输入联系人家庭住址（广东省汕头市...）：" ;
    getline(cin, home_address);
    address.set_home_address(home_address);
    cout << "请输入联系人单位住址（广东省汕头市...）：" ;
    getline(cin, unit_address);
    address.set_unit_address(unit_address);

    people_info_ptr->mutable_data()->PackFrom(address);

    cout << "请输入其他联系方式，1：QQ，2：微信，3：邮箱，4其他值：不添加其他联系方式：";
    int input;
    cin >> input;
    string qq, wechat, email;
    switch (input)
    {
        case 1: cout << "亲，输入QQ：";
            cin.ignore(256,'\n');
            getline(cin, qq);
            people_info_ptr->set_qq(qq);
            break;
        case 2: cout << "亲，输入微信：";
            cin.ignore(256,'\n');
            getline(cin, wechat);
            people_info_ptr->set_wechat(wechat);
            break;
        case 3: cout << "亲，输入邮箱地址：";
            cin.ignore(256,'\n');
            getline(cin, email);
            people_info_ptr->set_email(email);
            break;
        default:
            cin.ignore(256,'\n');
            break;
    }


    for(int i = 1; ; i++){
        cout << "请输入备注标题" << i << " [按下回车，完成备注新增]：";
        string remark_key;
        getline(cin, remark_key);
        if(remark_key.empty()) 
            break;
        string remark_value;
        cout << "请输入备注内容" << i << ": ";
        getline(cin, remark_value);
        people_info_ptr->mutable_remark()->insert({remark_key, remark_value});
    }
    
    cout << "-----------------添加联系人成功，success!---------------" << endl;

} 

int main(){
    contacts::Contacts contacts;
    // 向通讯录中添加一位联系人
    AddPeopleInfo(contacts.add_contacts());

    // 将通讯录写入到本地文件中
    fstream output("contacts.bin", ios::out | ios::app | ios::binary);
    if(!contacts.SerializeToOstream(&output)){
        cerr << "write error" << endl;
        return -2;
    }
    cout << "serialize success!" << endl;
    return 0;
}